// pages/notice/projectNotice/projectNotice.js
import { subscribe } from '../../../utils/tools.js';
import Http from '../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        uid: '',
        isIphone: false,
        createrealName: '',
        htmlContent: '',
        title: '',
        teamName: '',
        teamId: '',
        time: '',
        teamImg: '',
        readNumber: '',
        praiseflg: '', //点赞状态
        praisecnt: 0, //点赞数
        subscribeflg: '', //关注状态
        projectauthority: false, //是否为项目组成员
        //操作弹框
        show: false,
        actions: [
            {
                name: '删除',
                value: 'remark-delete',
                color: '#FF455E',
            },
        ],
        //评论
        remarkEdit: false,
        remarkId: '', //评论id
        commentList: [],
        commentcount: Number,
        toView: '', //跳转留言id
        commentTop: 101,
        tocommentid: '', //被回复的评论id,回复时传
        placeHoldComment: '欢迎留言讨论', //评论/回复内容
        touid: '',
        toreplyid: '',
        privateflg: '00',
        createuid: '',
        // 当前创建人uid
        activeCreateUid: '',
        recordCnt: 0, //编辑记录
    },
    recordBtn() {
        wx.navigateTo({
            url: `../../../releasePage/pages/announcePublish/announceList/announceList?id=${this.data.id}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _that = this;
        wx.getSystemInfo({
            success: function (res) {
                let isIpx;
                //model中包含着设备信息
                var model = res.model;
                if (model.search('iPhone') != -1) {
                    isIpx = true;
                } else {
                    isIpx = false;
                }
                _that.setData({
                    isIphone: isIpx,
                });
            },
        });
        _that.setData({
            id: options.id,
        });
        _that.addReadNum();
        _that.getProjectNoticeDetail();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    onReady() {
        this.footComment = this.selectComponent('#foot-comment');
    },
    //评论
    jumpOrRemark(e) {
        if (this.data.commentTop > 100) {
            this.setData({
                toView: 'comment',
                commentTop: 0,
            });
        }
        if (e.detail.setplaceHold == '00') {
            this.footComment.articalRemark();
            this.onResetInputContent();
        }
    },

    // 重置输入框内容
    onResetInputContent() {
        this.setData({
            placeHoldComment: '欢迎留言讨论',
            tocommentid: '',
            touid: this.data.createuid,
            privateflg: '00',
        });
    },

    scrollTopFun(e) {},

    remarkInfo(e) {
        let _that = this;
        _that.footComment.remarkFocus('01');
        let name = e.detail.battleTag;
        _that.setData({
            tocommentid: e.detail.contentid || e.detail.tocommentid, //被回复的评论id,回复时传
            placeHoldComment: '@' + name, //评论/回复内容
            touid: e.detail.touid,
            toreplyid: e.detail.toreplyid,
            privateflg: e.detail.privateflg,
        });
    },
    remarkEditChange(e) {
        this.setData({
            remarkEdit: e.detail.remarkEdit, //被回复的评论id,回复时传
        });
    },
    //取消
    onCancel(e) {
        this.setData({
            show: false,
        });
    },
    onDelect(e) {
        let _that = this;
        //是否有权限
        if (e.detail.value == 'remark-delete') {
            if (app.powerIdentity('00,01,02', _that.data.activeCreateUid))
                return;
            _that.setData({
                show: false,
            });
            wx.showModal({
                title: '提示',
                content: '确认要删除该项吗？',
                success: function (res) {
                    if (res.confirm) {
                        //评论删除
                        http.getRequest(
                            `cjMap/deleteComment?authId=${app.globalData.authId}&id=${_that.data.remarkId}`
                        ).then((res) => {
                            if (res.status == 0) {
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'none',
                                    duration: 1500,
                                });
                                _that.refreshRemark();
                            }
                        });
                    } else {
                        console.log('点击取消回调');
                    }
                },
            });
        }
    },
    //删除评论
    deleteRemark(e) {
        this.setData({
            show: true,
            actions: [
                {
                    name: '删除',
                    value: 'remark-delete',
                    color: '#FF455E',
                },
            ],
            remarkId: e.detail.id,
            activeCreateUid: e.detail.createuid,
        });
    },

    //关注
    subscribeBtn() {
        let _that = this;
        // wx.showToast({
        //     title: '该功能暂未开放',
        //     icon: 'none', //如果要纯文本，不要icon，将值设为'none'
        //     duration: 2000,
        // });
        let flg = '00';
        if (_that.data.subscribeflg == '00') {
            flg = '01';
        } else {
            if (_that.data.projectauthority) {
                wx.showToast({
                    title: '项目组成员不可以取消关注哦！',
                    icon: 'none',
                    duration: 2000,
                });
                return;
            }
        }
        subscribe(_that.data.teamId, flg, function () {
            wx.showToast({
                title: flg == '00' ? '已取消关注' : '关注成功',
                icon: 'none',
                duration: 2000,
            });
            _that.setData({
                subscribeflg: flg,
            });
        });
    },
    //操作评论后刷新
    refreshRemark() {
        let _that = this;
        http.getRequest(
            `cjMap/getProjectNoticeDetail?authId=${app.globalData.authId}&id=${_that.data.id}`
        ).then((res) => {
            if (res.status == 0) {
                _that.setData({
                    commentList: res.projectNotice.commentList,
                    commentcount: res.projectNotice.commentcount,
                    tocommentid: '', //被回复的评论id,回复时传
                    placeHoldComment: '欢迎留言讨论', //评论/回复内容
                });
            }
        });
    },
    //获取通告内容
    getProjectNoticeDetail() {
        let _that = this;
        http.getRequest(
            `cjMap/getProjectNoticeDetail?authId=${app.globalData.authId}&id=${_that.data.id}`
        ).then((res) => {
            if (res.status == 0) {
                _that.setData({
                    uid: res.projectNotice.createuid,
                    createuid: res.projectNotice.createuid,
                    htmlContent: res.projectNotice.text,
                    title: res.projectNotice.title,
                    teamName: res.projectNotice.pjteamname,
                    teamId: res.projectNotice.pjteamid,
                    time: app.timeago(res.projectNotice.createtime),
                    teamImg: res.projectNotice.pjteamlogopicurl,
                    readNumber: res.projectNotice.readnum,
                    createrealName: res.projectNotice.createrealname,
                    praiseflg: res.projectNotice.praiseflg,
                    praisecnt: res.projectNotice.praisecnt,
                    commentList: res.projectNotice.commentList,
                    commentcount: res.projectNotice.commentcount,
                    subscribeflg: res.projectNotice.subscribeflg,
                    projectauthority:
                        res.projectNotice.projectauthority == '02' ||
                        res.projectNotice.projectauthority == '03'
                            ? true
                            : false,
                    recordCnt: res.projectNotice.recordCnt,
                });
                let pages = getCurrentPages(); //当前页面栈
                let prevPage = pages[pages.length - 2]; //上一页面
                if (typeof prevPage.handleRead == 'function') {
                    prevPage.handleRead();
                }
            }
        });
    },
    //增加阅读量
    addReadNum() {
        let _that = this;
        http.getRequest(
            `cjMap/addProjectNoticeReadNumById?authId=${app.globalData.authId}&id=${_that.data.id}`
        ).then((res) => {
            if (res.status == 0) {
                console.log(res);
            }
        });
    },
});
